package com.zmn.brs.admin.controller.rule.order;


import com.alibaba.fastjson.JSONObject;
import com.zmn.brs.admin.controller.rule.base.AlarmRuleBaseController;
import com.zmn.brs.business.interfaces.rule.AlarmRuleBService;
import com.zmn.brs.common.enums.EventTypeEnum;
import com.zmn.brs.model.bo.rule.CustomRuleBO;
import com.zmn.brs.model.entity.event.Event;
import com.zmn.brs.model.entity.threshold.Threshold;
import com.zmn.brs.model.query.rule.ObjectiveRuleCustomQuery;
import com.zmn.brs.model.query.rule.OrderRuleCustomQuery;
import com.zmn.brs.model.vo.rule.ComparisonRuleVO;
import com.zmn.brs.model.vo.rule.QuantityRuleVO;
import com.zmn.brs.model.vo.rule.common.OrderRuleVO;
import com.zmn.brs.services.interfaces.event.EventService;
import com.zmn.brs.services.interfaces.threshold.ThresholdService;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


/**
 * 描述： 规则
 * @author chongw
 * @since 2021-05-28 17:05:29
 */
@Controller
@RequestMapping("/brs/order/rule/")
public class OrderRuleListController extends AlarmRuleBaseController {

    @Resource
    private EventService eventService;

    @Resource
    private AlarmRuleBService alarmRuleBService;

    @Resource
    private ThresholdService thresholdService;

    /**
     * 列表
     * @author chongw
     * @param query
     * @since 2021-05-28 17:05:29
     */
    @RequestMapping("list")
    public ModelAndView list(@ModelAttribute ObjectiveRuleCustomQuery query){
        Event event = eventService.findById(query.getEventId());
        ModelAndView mav = new ModelAndView();

        //判断是否人单比
        if(EventTypeEnum.COMPARISON.getTypeId().equals(event.getEventType())){
            View view = new RedirectView("/brs/order/rule/comparison/list.action?eventId=" + event.getEventId(),true,false);
            mav.setView(view);
            return mav;
        }
        //判断是否城市单量
        else if(EventTypeEnum.QUANTITY.getTypeId().equals(event.getEventType())){
            View view = new RedirectView("/brs/order/rule/quantity/list.action?eventId=" + event.getEventId(),true,false);
            mav.setView(view);
            return mav;
        }
        //呼叫进线量
        else{
            View view = new RedirectView("/brs/order/rule/calls/list.action?eventId=" + event.getEventId(),true,false);
            mav.setView(view);
            return mav;
        }
    }

    /**
     * 人单比规则列表
     * @author chongw
     * @param query
     * @since 2021-05-28 17:05:29
     */
    @RequestMapping("comparison/list")
    public ModelAndView comparisonList(@ModelAttribute OrderRuleCustomQuery query){
        List<CustomRuleBO> items = this.getOrderRuleList(query, ComparisonRuleVO.class);
        Event event = eventService.findById(query.getEventId());
        //根据事件查询规则Id
        Integer ruleId = alarmRuleBService.findRuleIdByEventId(query.getEventId());
        ModelAndView mv = new ModelAndView("order/rule/comparison/list");
        mv.addObject("data" , event);
        mv.addObject("vo" , query);
        mv.addObject("items" , items);
        mv.addObject("sceneId" , event.getSceneId());
        mv.addObject("eventId" , event.getEventId());
        mv.addObject("flag" , 2);
        mv.addObject("ruleId" , ruleId);
        return mv;
    }

    /**
     * 人单比规则列表
     * @author chongw
     * @param query
     * @since 2021-05-28 17:05:29
     */
    @RequestMapping("comparison/addEditView")
    public ModelAndView comparisonDetail(@ModelAttribute OrderRuleCustomQuery query){
        Event event = eventService.findById(query.getEventId());
        CustomRuleBO data = CustomRuleBO.builder()
                .data(ComparisonRuleVO.builder().build())
                .build();
        if(!NumberUtil.isNullOrZero(query.getThresholdId())){
            List<CustomRuleBO> items = this.getOrderRuleList(query, ComparisonRuleVO.class);
            if(!items.isEmpty()){
                data = items.get(0);
            }
        }
        ModelAndView mv = new ModelAndView("order/rule/comparison/addEdit");
        mv.addObject("data" , data);
        mv.addObject("eventId" , event.getEventId());
        return mv;
    }

    /**
     * 城市单量规则列表
     * @author chongw
     * @param query
     * @since 2021-05-28 17:05:29
     */
    @RequestMapping("quantity/list")
    public ModelAndView quantityList(@ModelAttribute OrderRuleCustomQuery query){
        List<CustomRuleBO> items = this.getOrderRuleList(query , QuantityRuleVO.class);
        Event event = eventService.findById(query.getEventId());
        //根据事件查询规则Id
        Integer ruleId = alarmRuleBService.findRuleIdByEventId(query.getEventId());
        ModelAndView mv = new ModelAndView("order/rule/quantity/list");
        mv.addObject("data" , event);
        mv.addObject("vo" , query);
        mv.addObject("items" , items);
        mv.addObject("sceneId" , event.getSceneId());
        mv.addObject("eventId" , event.getEventId());
        mv.addObject("ruleId" , ruleId);
        mv.addObject("flag" , 2);
        return mv;
    }

    /**
     * 城市单量详情
     * @author chongw
     * @param query
     * @since 2021-06-25 17:05:29
     */
    @RequestMapping("quantity/addEditView")
    public ModelAndView quantityDetail(@ModelAttribute OrderRuleCustomQuery query){
        Event event = eventService.findById(query.getEventId());
        CustomRuleBO data = CustomRuleBO.builder()
                .data(QuantityRuleVO.builder().build())
                .build();
        if(!NumberUtil.isNullOrZero(query.getThresholdId())){
            List<CustomRuleBO> items = this.getOrderRuleList(query , QuantityRuleVO.class);
            if(!items.isEmpty()){
                data = items.get(0);
            }
        }
        ModelAndView mv = new ModelAndView("order/rule/quantity/addEdit");
        mv.addObject("data" , data);
        mv.addObject("eventId" , event.getEventId());
        return mv;
    }

    /**
     * 公共阈值
     * @author chongw
     * @param query
     * @since 2021-07-12 16:52:29
     */
    @RequestMapping("common/addEditView")
    public ModelAndView commonDetail(@ModelAttribute OrderRuleCustomQuery query){
        Threshold data = Optional.ofNullable(thresholdService.findCommonThresholdByRuleId(query.getRuleId())).orElse(Threshold.builder().build());
        OrderRuleVO threshold = OrderRuleVO.builder().build();
        if(StringUtil.isNotBlank(data.getThresholdValue())){
            threshold = JSONObject.parseObject(data.getThresholdValue(), OrderRuleVO.class);
        }
        ModelAndView mv = new ModelAndView("order/rule/common/addEdit");
        mv.addObject("data" , data);
        mv.addObject("threshold" , threshold);
        mv.addObject("ruleId" , query.getRuleId());
        mv.addObject("eventId" , query.getEventId());
        return mv;
    }

    /**
     * 进线量规则列表
     * @author chongw
     * @param query
     * @since 2021-05-28 17:05:29
     */
    @RequestMapping("calls/list")
    public ModelAndView callsList(@ModelAttribute OrderRuleCustomQuery query){
        Event event = eventService.findById(query.getEventId());
        ModelAndView mv = new ModelAndView("order/rule/calls/list");
        mv.addObject("data" , event);
        mv.addObject("sceneId" , event.getSceneId());
        mv.addObject("eventId" , event.getEventId());
        mv.addObject("flag" , 2);
        return mv;
    }

    /**
     * 获取订单监控规则
     * @param query
     * @return
     */
    protected List<CustomRuleBO> getOrderRuleList(OrderRuleCustomQuery query, Class clazz){
        //设置监控周期
        if (StringUtil.isNotBlank(query.getTimeSearch())) {
            String[] timeRangeArray = query.getTimeSearch().split("-");
            query.setStartTime(timeRangeArray[0].trim() + " 00:00:00");
            query.setEndTime(timeRangeArray[1].trim() + " 23:59:59");
        }
        //查询人单比配置数据
        List<CustomRuleBO> items = Optional.ofNullable(alarmRuleBService.listPageOrderRuleByEventQuery(query)).orElse(new ArrayList<>());
        items.stream().forEach(o -> {
            o.setData(JSONObject.parseObject(o.getThresholdValue(), clazz));
        });
        return items;
    }

}
